/*
 * This file is part of SND.
 *
 * Sensor Network Deployer
 * Copyright (C)  Michael Morckos 2008 <mikey.morckos@gmail.com>
 * 
 * SND is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SND is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with SND.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Deployer_UI extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	private JPanel control_panel;
	private JTabbedPane tPane;
	private JLabel [] labels;
	private JTextField [] texts;
	private JComboBox combo;
	private JButton deploy_nodes, draw_edges, reset;
	
	private String [] graph_names;
	private Graph_Panel [] graph_panels;
	
	/* DEPLOYER */
	private Deployer dep;

	public Deployer_UI() {

		dep = new Deployer();		
		init();
	}
	
	public void init() {
		
		setBounds(10, 5, 1000, 700);
		setTitle("Deployer");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
		getContentPane().setBackground(new Color(134, 160, 187));
		
		control_panel();
		graph_panel();
		
		setVisible(true);
	}
	
	public void control_panel() {
		
		control_panel = new JPanel(null);
		control_panel.setBounds(0, 0, 200, 760);
		getContentPane().add(control_panel);
		
		/* Labels */
        labels = new JLabel[7];
		
		labels[0] = new JLabel("Node Data :-");
		labels[0].setFont(new Font("Century Gothic", Font.BOLD, 13));
		labels[0].setBounds(5, 5, 85, 30);
		control_panel.add(labels[0]);
		
		labels[1] = new JLabel("T Range :-");
		labels[1].setFont(new Font("Century Gothic", Font.BOLD, 12));
		labels[1].setBounds(10, 40, 80, 20);
		control_panel.add(labels[1]);
		
		labels[2] = new JLabel("No. of Nodes :-");
		labels[2].setFont(new Font("Century Gothic", Font.BOLD, 12));
		labels[2].setBounds(10, 60, 100, 20);
		control_panel.add(labels[2]);
		
		labels[3] = new JLabel("Deployment Area :-");
		labels[3].setFont(new Font("Century Gothic", Font.BOLD, 13));
		labels[3].setBounds(5, 100, 135, 20);
		control_panel.add(labels[3]);
		
		labels[4] = new JLabel("Width :-");
		labels[4].setFont(new Font("Century Gothic", Font.BOLD, 12));
		labels[4].setBounds(10, 130, 70, 20);
		control_panel.add(labels[4]);
		
		labels[5] = new JLabel("Height :-");
		labels[5].setFont(new Font("Century Gothic", Font.BOLD, 12));
		labels[5].setBounds(10, 150, 70, 20);
		control_panel.add(labels[5]);
		
		labels[6] = new JLabel("Probability :-");
		labels[6].setFont(new Font("Century Gothic", Font.BOLD, 13));
		labels[6].setBounds(5, 190, 135, 20);
		control_panel.add(labels[6]);
		
		/* Text Fields */
        texts = new JTextField[5];
		
		texts[0] = new JTextField(15);
		texts[0].setBounds(120, 40, 50, 20);
		control_panel.add(texts[0]);
		
		texts[1] = new JTextField(15);
		texts[1].setBounds(120, 60, 50, 20);
		control_panel.add(texts[1]);
		
		texts[2] = new JTextField(15);
		texts[2].setBounds(120, 130, 50, 20);
		control_panel.add(texts[2]);
		
		texts[3] = new JTextField(15);
		texts[3].setBounds(120, 150, 50, 20);
		control_panel.add(texts[3]);
		
		texts[4] = new JTextField(15);
		texts[4].setBounds(120, 190, 50, 20);
		control_panel.add(texts[4]);
		
		/* Combo */
		graph_names = new String [4];
		graph_names[0] = "Geometric G(n,R)";
		graph_names[1] = "Geometric G(n,K)";
		graph_names[2] = "Geometric G(n,p,R)";
		graph_names[3] = "Bernoulli G(n, p)";
		
		combo = new JComboBox(graph_names);
		combo.setSelectedIndex(0);
		combo.setBounds(5, 230, 190, 20);
		control_panel.add(combo);
		
		/* Buttons */
		deploy_nodes = new JButton("Deploy Nodes");
		deploy_nodes.setBackground(new Color(110, 222, 140));
		deploy_nodes.setBounds(30, 280, 130, 20);
		deploy_nodes.addActionListener(this);
		control_panel.add(deploy_nodes);
		
		reset = new JButton("Reset");
		reset.setBackground(new Color(203, 64, 66));
		reset.setBounds(30, 305, 130, 20);
		reset.addActionListener(this);
		control_panel.add(reset);
		
		getContentPane().add(control_panel);
	}
	
	public void graph_panel() {
		
		graph_panels = new Graph_Panel[4];
		
		for(int i = 0; i < 4; i++)
			graph_panels[i] = new Graph_Panel();
			
		tPane = new JTabbedPane();
		tPane.setBounds(200, 0, 800, 680);
		
		for(int i = 0; i < 4; i++)
			tPane.add(graph_names[i], graph_panels[i]);
		
		getContentPane().add(tPane);
	}
	
	public void actionPerformed(ActionEvent e) {
		
		Graph_Panel temp;
		int mode;
		
		if(combo.getSelectedItem().equals(graph_names[0])) {
			temp = graph_panels[0];
			mode = 0;
		}
		else if(combo.getSelectedItem().equals(graph_names[1])) {
			temp = graph_panels[1];
			mode = 1;
		}
        else if(combo.getSelectedItem().equals(graph_names[2])) {
			temp = graph_panels[2];
			mode = 2;
		}
        else {
			temp = graph_panels[3];
			mode = 3;
		}
		
		if(e.getActionCommand().equalsIgnoreCase("Deploy Nodes"))
			dep.deployNodes((Integer.parseInt(texts[0].getText())), (Integer.parseInt(texts[1].getText())), 
					        (Integer.parseInt(texts[2].getText())), (Integer.parseInt(texts[3].getText())),
					        (Float.parseFloat(texts[4].getText())), mode, temp);
		else
			dep.reset(graph_panels[tPane.getSelectedIndex()]);
	}
}

